package com.devbobcorn.nekoration.blocks;

import java.util.Random;

import javax.annotation.Nullable;

import com.devbobcorn.nekoration.blocks.entities.CabinetBlockEntity;

import net.minecraft.block.Block;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.BlockState;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.monster.piglin.PiglinTasks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.ItemStack;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;

public class CabinetBlock extends DyeableHorizontalWoodenBlock {
	public final boolean large;
	public static final BooleanProperty OPEN = BlockStateProperties.OPEN;

	public CabinetBlock(AbstractBlock.Properties settings, boolean l) {
		super(settings);
		this.large = l;
		this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(OPEN, Boolean.valueOf(false)));
	}

    public void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> s) {
        s.add(COLOR, FACING, OPEN);
    }

	@Override
	public boolean hasTileEntity(BlockState state) {
		return true;
	}

	@Override
	public TileEntity createTileEntity(BlockState state, IBlockReader world) {
		return new CabinetBlockEntity(large);
	}

	public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult result) {
		if (world.isClientSide) {
			return ActionResultType.SUCCESS;
		} else {
			TileEntity tileentity = world.getBlockEntity(pos);
			if (tileentity instanceof CabinetBlockEntity) {
				player.openMenu((CabinetBlockEntity) tileentity);
				PiglinTasks.angerNearbyPiglins(player, true);
			}
			return ActionResultType.CONSUME;
		}
	}

	public void onRemove(BlockState state, World world, BlockPos pos, BlockState newState, boolean isMoving) {
		if (!state.is(newState.getBlock())) {
			TileEntity tileentity = world.getBlockEntity(pos);
			if (tileentity instanceof IInventory) {
				InventoryHelper.dropContents(world, pos, (IInventory) tileentity);
				world.updateNeighbourForOutputSignal(pos, this);
			}
			world.removeBlockEntity(pos);
		}
	}

	public void tick(BlockState state, ServerWorld world, BlockPos pos, Random rand) {
		TileEntity tileentity = world.getBlockEntity(pos);
		if (tileentity instanceof CabinetBlockEntity) {
			((CabinetBlockEntity) tileentity).recheckOpen();
		}
	}

	public void setPlacedBy(World world, BlockPos pos, BlockState state, @Nullable LivingEntity entity, ItemStack stack) {
		if (stack.hasCustomHoverName()) {
			TileEntity tileentity = world.getBlockEntity(pos);
			if (tileentity instanceof CabinetBlockEntity) {
				((CabinetBlockEntity) tileentity).setCustomName(stack.getHoverName());
			}
		}
	}

	public boolean hasAnalogOutputSignal(BlockState state) {
		return true;
	}

	public int getAnalogOutputSignal(BlockState state, World world, BlockPos pos) {
		return Container.getRedstoneSignalFromBlockEntity(world.getBlockEntity(pos));
	}

	@Nullable
	public INamedContainerProvider getMenuProvider(BlockState state, World world, BlockPos pos) {
		TileEntity tileentity = world.getBlockEntity(pos);
		return tileentity instanceof INamedContainerProvider ? (INamedContainerProvider) tileentity : null;
	}
}